package com.sphd.emanu.fragment.goods;

import android.support.v7.app.AlertDialog;

import com.google.gson.JsonObject;
import com.sphd.emanu.mvp.BasePresenterImpl;
import com.sphd.emanu.pojo.goods.Commodity;
import com.sphd.emanu.pojo.material.CommodityAndCategory;
import com.sphd.emanu.utils.GsonUtil;
import com.wzutils.L;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class GoodsPresenter extends BasePresenterImpl<GoodsContract.View> implements GoodsContract.Presenter {

    @Override
    public void commodityStock(Integer org, Integer status, Integer curpage, Integer pageno) {
        emanuService.commodityStock(org, status, curpage, pageno).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                L.e(response.toString());
                CommodityAndCategory category = GsonUtil.GsonToBean(response.body(), CommodityAndCategory.class);
                mView.setBean(category);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void commodityStockByCategory(Integer id, Integer status, Integer curpage, Integer pageno) {
        emanuService.commodityStockByCategory(id, status, curpage, pageno).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                L.e(response.toString());
                List<Commodity> commodityList = GsonUtil.jsonToList(response.body(), Commodity.class);
                mView.setBeanList(commodityList);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void updateCommodity(int id, Integer reserveStock, int pos, AlertDialog alertDialog) {
        emanuService.updateCommodity(id, reserveStock).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                L.e(response.toString());
                mView.setRes(res(response),pos,reserveStock,alertDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }
}
